/**
 * @作者 chengxinxiaofeng
 * @时间 2019年7月15日10:30:20
 */
package com.chinaunicom.tour.sys.service;

import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.chinaunicom.tour.sys.entity.AirplanTicket;
import com.chinaunicom.tour.sys.entity.BuyAirplanTicket;
import com.chinaunicom.tour.sys.mapper.AirplanTicketMapper;
import com.chinaunicom.tour.sys.mapper.BuyAirplanTicketMapper;
import com.chinaunicom.tour.sys.model.BuyAirplanTicketModel;

@Service
public class BuyAirplanTicketService {

    @Autowired
    BuyAirplanTicketMapper buyAirplanTicketMapper;
    
    @Autowired
    AirplanTicketMapper airplanTicketMapper;

	//添加事务，报错则回滚
	@Transactional(rollbackFor = Exception.class)
    public Boolean add(BuyAirplanTicket entity) {
		AirplanTicket airplanTicket = airplanTicketMapper.findById(entity.getAirplanTicketId());
		
		//航班不存在或没有余票
		if(airplanTicket == null  || 
	      airplanTicket.getRemainCount() == null  ||
		  airplanTicket.getRemainCount() <= 0){
			return false;
		}
		entity.setCreateTime(new Date());
		//购买机票表插入数据
		buyAirplanTicketMapper.add(entity);
		//机票余数减1
		airplanTicketMapper.updateRemainCount();
		
		return true;
    }

    public int delete(String id) {
        return buyAirplanTicketMapper.delete(id);
    }

    public int update(BuyAirplanTicket entity) {
        return buyAirplanTicketMapper.update(entity);
    }

    public BuyAirplanTicket findById(String id) {
        return buyAirplanTicketMapper.findById(id);
    }

    public List<BuyAirplanTicket> findList(BuyAirplanTicketModel queryModel) {
        return buyAirplanTicketMapper.findList(queryModel);
    }

    public int pageQueryCount(BuyAirplanTicketModel queryModel) {
        return buyAirplanTicketMapper.pageQueryCount(queryModel);
    }

    public List<BuyAirplanTicket> pageQuery(BuyAirplanTicketModel queryModel) {
        return buyAirplanTicketMapper.pageQuery(queryModel);
    }

}
